<template>
	<view class="container">
		<view style="width: 100%;padding: 20rpx;height: 100rpx;margin-top: 20rpx; display: none;">
			<!-- 搜索框导航栏名称变成'搜索' -->
			<view class="input-group">
				<input placeholder="请输入关键字搜索..." v-model="name" :focus="getIptFocus" @blur="clearIptFocus" />
				<image class="searchicon" src="/static/image/searchicon1.png" mode=""></image>
				<image class="searchClear" src="/static/image/x1.png" v-show="searchClear" @tap="clearIpt"></image>
			</view>
			<view class="searchright" @click="searchClick()">
				搜索
			</view>
		</view>
		<!-- 专家展示 -->
        
        
                 <!-- #ifdef MP-WEIXIN -->

          <button @click="go">选择</button>
        <!-- #endif -->
		<view class="BusinessMain">
			<block v-for="(item, index) in itemdata" :key="index">
				<view style="width: 100%;height: 80rpx; padding-left: 20rpx;line-height: 80rpx;font-size: 35rpx;font-weight: bold;"> {{ item.name }}</view>
				<view style="width: 100%;padding: 0rpx 20rpx;">							
					<checkbox-group @change="checkboxChange">
						<block v-for="(item1, index) in item.children" :key="index">
							<view class="item">
								<view class="middle">
									<view class="title">{{ item1.name }}</view>
								</view>
								<view class="right">
									<checkbox :value="'check_'+ item1.id" :checked="item1.checked"  color="#1cbbb4">
									</checkbox>
								</view>
							</view>
						</block>
					</checkbox-group>
				</view>
			</block>
		</view>
        
       

	</view>
</template>

<script>
	import myExpert from '@/components/expert/expert.vue';
	export default {
		components: {
			myExpert
		},
		data() {
			return {
				name: '',
				getIptFocus: false, //清除完内容自动获取焦点
				searchClear: false, //是否显示清除按钮
				
				// 数据
				itemdata:{},
				checkedArr:[],
				checkedArrid:[],

			};
		},
		onLoad(event) {
			// uni.startPullDownRefresh();		
		},
		
		async onShow(options) {
			await this.initData();
		},
		
		onNavigationBarButtonTap(e) {
			uni.$emit('returnItem', {
				data:this.checkedArr,
				ids:this.checkedArrid,
			});
			uni.navigateBack({
				delta:1
			})
		},
		//下拉刷新
		onPullDownRefresh() {
			uni.stopPullDownRefresh();
		},
		//上拉加载
		onReachBottom() {

		},
		watch: {
			name(n) {
				if (n.length > 0) {
					this.searchClear = true;
					console.log(this.name);
				} else {
					this.searchClear = false;
					this.name = "";
				}
			}
		},
		methods: {
            go(){
                uni.$emit('returnItem', {
                	 data:this.checkedArr
                });
                uni.navigateBack({
                	delta:1
                })
            },
			// 点击清除所有
			clearIpt() {
				this.name = '';
				this.getIptFocus = true;
			},
			// 失去焦点
			clearIptFocus() {
				this.getIptFocus = false;
				console.log(this.name);
			},
			// 搜索按钮
			searchClick() {
				uni.showToast({
					title: this.name
				})
			},
			
			// 数据初始化
			async initData() {
				let that = this;			
			    let user = uni.getStorageSync('user');
			    // console.log(token)
			    uni.request({
			        method: 'GET',
			        url: 'http://nmt.yunshangwulian.vip/api/departments' + "?api_token=" + user
			            .api_token, //仅为示例，并非真实接口地址。
			        data: {
			
			        },
			        header: {
			            'Accept': 'application/json' //自定义请求头信息
			        },
			        success: (res) => {
			            // console.log(res.data);
						this.itemdata = res.data;
			        }
			    });
			},
			
			
			checkboxChange: function(e) {
				var items = this.itemdata,
					values = e.detail.value;
				this.checkedArr = [];
				this.checkedArrid = [];
				for (var i = 0, lenI = items.length; i < lenI; ++i) {
					var item1 = items[i].children;
					for (var k = 0, lenk = item1.length;k < lenk; ++k) {
						const item = item1[k];
						if (values.includes("check_" + item.id)) {
							this.$set(item, 'checked', true)
						} else {
							this.$set(item, 'checked', false)
						}
						if( item.checked){
							this.checkedArr.push( item )
							this.checkedArrid.push( item.id )
						}
					}
				}
			},
		}
	};
</script>

<style lang="scss">
	
	.uni-checkbox-input::before,
	.uni-radio-input::before {
		display: block;
	}
	
	.container {
		width: 100%;
		min-height: 100vh;
		background-color: #F7F7F7;
	}

	.input-group {
		position: relative;
		float: left;
		width: 88%;
		border-radius: 50rpx;
		margin-top: 10rpx;
		color: #FFFFFF;


		input {
			// border: 1upx solid gray;
			height: 90rpx;
			padding-left: 70rpx;
			font-size: 30upx;
			box-sizing: border-box;
			border-radius: 100rpx;
			background-color: #FFFFFF;
			color: #000000;
		}

		.uni-input-placeholder {
			color: #ACACAC;
		}

		.searchicon {
			left: 20rpx;
			position: absolute;
			width: 32rpx;
			height: 32rpx;
			top: 28rpx;
		}

		.searchClear {
			right: 20rpx;
			z-index: 2;
			position: absolute;
			width: 32rpx;
			height: 32rpx;
			top: 28rpx;
		}
	}

	.searchright {
		position: relative;
		float: right;
		width: 12%;
		color: #222222;
		text-align: center;
		font-size: 35rpx;
		line-height: 100rpx;
	}


	.item {
		margin-top: 20rpx;
		height: 100rpx;
		background: #ffffff;
		border-radius: 10rpx;


		.left {
			width: 25%;
			float: left;
			text-align: center;

			.icon {
				margin-top: 20rpx;
				width: 110rpx;
				height: 110rpx;
			}
		}

		.middle {
			width: 90%;
			// height: 110rpx;
			float: left;
			padding-left: 20rpx;
			margin-top: 20rpx;

			.title {
				line-height: 55rpx;
				font-size: 30rpx;
				font-weight: bold;
				color: #1e1e1e;
			}
		}

		.right {
			width: 5%;
			float: left;
			margin-top: 20rpx;
		}
	}
</style>
